package com.lenovo.vod.player.setting.util;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lenovo.vod.player.setting.util.MediaBaseArrayAdapter;
import com.lenovo.vod.player.setting.util.StringUtils;
import com.lenovo.vod.player.R;

/**
 *	Adapter
 * 
 * @author lenovo
 * 
 */
public class MediaBaseArrayAdapter extends BaseAdapter {

    private List<String> mObjects;
    private Context mContext;
    private LayoutInflater mInflater;
    private String mObjectName;
    private List<String> mCurrents;

    public MediaBaseArrayAdapter(Context context, String[] objects,
            String textArrayValue,List<String> currents) {
        init(context, Arrays.asList(objects), textArrayValue,currents);
    }

    public MediaBaseArrayAdapter(Context context, String[] objects) {
        // init(context , Arrays.asList(objects),null);
        this.mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mObjects = Arrays.asList(objects);
    }

    public MediaBaseArrayAdapter(Context context) {
        this.mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * change adapter data
     * @param objects
     */
    public void changeDatas(List<String> objects) {
        this.mObjects = objects;
        this.notifyDataSetChanged();
    }

    /**
     * change adapter data
     * @param textArrayRecourseId
     */
    public void changeData(int textArrayRecourseId) {
        String[] strArr = mContext.getResources().getStringArray(
                textArrayRecourseId);
        this.mObjects = Arrays.asList(strArr);
        this.notifyDataSetChanged();
    }

    private void init(Context context, List<String> objects,
            String textArrayValue , List<String> currents ) {
        mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mObjects = objects;
        mObjectName = textArrayValue;
        mCurrents = currents;
        
    }

    public int getCount() {
        return mObjects.size();
    }

    public String getItem(int position) {
        return mObjects.get(position);
    }

    public int getPosition(String item) {
        return mObjects.indexOf(item);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            view = mInflater.inflate(
                    R.layout.vodplayer_setting_option_listitems, parent, false);
        } else {
            view = convertView;
        }

        TextView text = (TextView) view
                .findViewById(R.id.vodplayer_setting_tvOption);
        String item = getItem(position);
        /*
        text.setTextColor(Color.WHITE);

        String item = getItem(position);
        String current = mCurrents.get(position);

        if (mObjectName != null && !"".equals(mObjectName)) {
            if (mObjectName.equals(current)) {
                text.setTextColor(Color.BLACK);
            }
        }
	*/
        if (item instanceof CharSequence) {
            text.setText((CharSequence) item);
        } else {
            text.setText(item.toString());
        }

        return view;

    }

    public static MediaBaseArrayAdapter createFromResource(Context context,
            int textArrayResId) {
        String[] strings = context.getResources()
                .getStringArray(textArrayResId);
        return new MediaBaseArrayAdapter(context, strings);
    }

    public static MediaBaseArrayAdapter createFromResource(Context context,
            int textArrayResId, String textArrayValue,String [] currents) {
        String[] strings = context.getResources()
                .getStringArray(textArrayResId);
        List<String> currentList = StringUtils.getStringArrayByResourceId(currents);
        return new MediaBaseArrayAdapter(context, strings, textArrayValue,currentList);
    }
}
